package com.fasteat.menu.async;

import com.fasteat.menu.service.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author run
 * @since 2021/3/8 21:47
 */
@Component
public class RecommendAsync {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserClient userClient;

    @Value("${spring.redis.expire-time}")
    private String expireTime;


    @Async("asyncTaskExecutor")
    public void insertKeyInRedis(String userId){
        Object userList = userClient.getAuthorVos(Arrays.asList(new String[]{userId})).getData();

        //如果没有此用户，直接返回
        if (userList == null || userList.toString().equals("[]")){
            return;
        }

        String redisKey = "Recommend_" + userId;
        redisTemplate.opsForSet().add(redisKey,"");
        redisTemplate.expire(redisKey,Long.valueOf(expireTime), TimeUnit.SECONDS);
    }
}
